<?php

/**
 * Student form.
 *
 * @package    elearning
 * @subpackage form
 * @author     Your name here
 */
class StudentForm extends BaseStudentForm
{
  public function configure()
  {
	$this->widgetSchema['std_avatar_image'] = new sfWidgetFormInputFileEditable(array(
	   'label'     => 'Student Avatar',
	   'file_src'  => '/uploads/assets/student/'.$this->getPicPath().'/thumb_'.$this->getObject()->getStdAvatarImage(),
	   'is_image'  => true,
	   'edit_mode' => !$this->isNew(),
	   'template'  => '<div><p>%file%</p><p>%input%</p><p>%delete% %delete_label%</p></div>',
	));
	
	$this->validatorSchema['std_avatar_image'] = new sfValidatorFile(array(
	   'required'   => !((bool)($this->getObject()->getStdAvatarImage())),
	   'mime_types' => 'web_images',
	   'path' => sfConfig::get('sf_upload_dir').'/assets/student/'.$this->getPicPath(),
	   'validated_file_class' => 'sfResizedFile',
	   'max_size'   => 41943040,
	));
	//fix status vieew
	$status = $this->getObject();
	if( $status->getStdStatus() == 0 )
		$status->setStdStatus(NULL);
	$this->validatorSchema['std_status'] =  new sfValidatorInteger( array('required' => false ));
	$this->widgetSchema['std_status'] = new sfWidgetFormInputCheckbox(array("value_attribute_value" => 1),array('value' => 1));
	//$this->widgetSchema['std_status'] = new sfWidgetFormInputCheckbox();
	//$this->validatorSchema['std_status'] =  new sfValidatorInteger( array('required' => false ));
	
	
	//password
	$this->widgetSchema['std_password'] = new sfWidgetFormInputPassword();
	$this->validatorSchema['std_email'] = new sfValidatorEmail();
	if( $this->isNew() )
	{
		
		$this->setDefault('std_last_vistit',time());
	}
	else
	{
		//unset( $this['std_password'] );
		$this->validatorSchema['std_password']  = new sfValidatorString(array('max_length' => 45, 'required' => false, 'min_length' => 6 ));
	}
	unset( $this['std_last_vistit'], $this['created_at'], $this['updated_at'] );
	/*
	$this->widgetSchema['email_again'] = new sfWidgetFormInput(); 
    $this->widgetSchema->moveField('email_again', 'after', 'std_email');  
    # Edit default validator Schema  
    $this->validatorSchema['std_email'] = new sfValidatorEmail();   
    $this->validatorSchema['email_again'] = clone $this->validatorSchema['std_email'];  
    $this->mergePostValidator(new sfValidatorSchemaCompare('std_email', sfValidatorSchemaCompare::EQUAL, 'email_again', array(), array('invalid' => 'The two e-mails must be the same.')));     
    $this->validatorSchema->addOption('allow_extra_fields', true);  

	//$this->setDefault('email_again',$this['std_password'] );
	//sfWidgetFormInputPassword()
	$this->widgetSchema['std_password'] = new sfWidgetFormInputPassword();
    $this->widgetSchema['password_again'] = new sfWidgetFormInputPassword();
    $this->widgetSchema->moveField('password_again', 'after', 'std_password');  
    // Edit default validator Schema  
	$this->validatorSchema['password_again'] = clone $this->validatorSchema['std_password'];
    $this->mergePostValidator(new sfValidatorSchemaCompare('std_password', sfValidatorSchemaCompare::EQUAL, 'password_again', array(), array('invalid' => 'The two passwords must be the same.')));  
	*/
	
	$this->widgetSchema->setNameFormat('student[%s]');
    //$this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
  }
  
  protected function getPicPath()
  {
	return utilities::getSpreadPathByDate($this->getObject()->getCreatedAt());
  }   
}
